/**
 * @file ChangeLayerCommand.cpp
 *
 * @date 30/12/2011
 * @author Tales 
 *
 */

#include "ChangeLayerCommand.hpp"
#include "PictureState.hpp"

namespace yep
{

ChangeLayerCommand::ChangeLayerCommand(size_t src, size_t dst, bool swap) :
		Command(Color::BLACK, src)
{
	source = src;
	destiny = dst;
	this->swap = swap;
}

void ChangeLayerCommand::execute(PictureState & state)
{
	if (source == destiny)
		return;
	if (swap)
		state.swap(source, destiny);
	else
	{
		if (source < destiny)
		{
			size_t delta = destiny - source;
			for (size_t i = 0; i < delta; ++i)
				state.swap(source + i, source + i + 1);
		}
		else // source > destiny
		{
			size_t delta = source - destiny;
			for (size_t i = 0; i < delta; ++i)
				state.swap(source - i, source - i - 1);
		}
	}
}

} /* namespace yep */
